package com.canbot.u05.activity.firstguide

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.util.Base64
import android.view.View
import android.widget.EditText
import android.widget.Toast

import com.canbot.u05.R
import com.canbot.u05.activity.BaseActivityNoTitle
import com.canbot.u05.activity.showToast
import com.canbot.u05.socket.MsgSendUtils
import com.canbot.u05.socket.MsgType
import com.canbot.u05.utils.PathConst
import kotlinx.android.synthetic.main.activity_password_set.*

/**
 * desc: 首次引导-管理员密码设置界面
 * author: xxl
 * date: 2019/6/19
 */
class PasswordSetActivity : BaseActivityNoTitle() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_password_set)

        btn_next_step.setOnClickListener {
            val newPassword = et_admin_password.text.toString()
            val confirmPassword = et_confirm_password.text.toString()

            if (newPassword.isEmpty()) {
                showToast(getString(R.string.password_empty_tip))
                return@setOnClickListener
            }

            if (newPassword.length < PASSWORD_LENGTH) {
                showToast(getString(R.string.password_length_tip))
                return@setOnClickListener
            }

            if (confirmPassword != newPassword) {
                showToast(getString(R.string.twice_password_diffent_tip))
                return@setOnClickListener
            }

            //密码Base64加密
            val encodePsw = Base64.encodeToString(confirmPassword.toByteArray(), Base64.DEFAULT)

            val intent = Intent(this@PasswordSetActivity, SceneChooseActivity::class.java)
            intent.putExtra("password", encodePsw)
            startActivity(intent)
        }
    }

    override fun onResume() {
        super.onResume()
        MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, PathConst.ADMIN_AUDIO_PATH + "glyscyz_szscyd_u05_001.mp3")
    }

    override fun onBackPressed() {

    }

    companion object {
        private const val PASSWORD_LENGTH = 6
    }
}
